package team.zero.lemon.module.cms.tools;

import team.zero.lemon.common.date.UTC;

import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.util.Dictionary;
import java.util.Iterator;
import java.util.Map;

/**
 * Created by bolin on 2016/10/25.
 */
public class Html {

    public static boolean Create(String templatePath, String path, String fileName, Map map) {
        String str = "";
        try {
            String tempStr = "";
            FileInputStream is = new FileInputStream(templatePath);//读取模块文件
            BufferedReader br = new BufferedReader(new InputStreamReader(is));
            while ((tempStr = br.readLine()) != null)
                str = str + tempStr;
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
        try {
            Iterator iter = map.entrySet().iterator();
            while (iter.hasNext()) {
                Map.Entry entry = (Map.Entry) iter.next();
                Object key = entry.getKey();
                Object val = entry.getValue();
                str = str.replaceAll(String.format("\\$%s\\$",key),val.toString());
            }
            File f = new File(path+File.separator+fileName);
            FileOutputStream fos = new FileOutputStream(path+File.separator+fileName);
            OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");
            osw.write(str);
            osw.flush();
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
        return true;

    }
}
